Jump to content

Ammo Pickup - what am I doing wrong?


Angelwolf
 Share

Recommended Posts

I'm trying to make a simple script whereby the player can collect ammo for his weapon.

function Script:Use(player)
    if player.weapons[1].ammo<player.weapons[1].maxammo then
    player.weapons[1].ammo= player.weapons[1].ammo + self.ammoamount
    end
end
	

This works but the player must look at the item and 'use' it for it to activate. When I try to turn this into a collision script, it doesn't work.

Can anyone help me adjust this so that it will give the player ammo on collision?

This was hacked up, edited and simplified from Haydenmango's old script from 2014. My weapon script has a variable of maxammo. It works on 'use' but for the life of me, I cannot translate it to collision.

Link to comment
Share on other sites

You'll need a way for the ammo item to tell that the colliding entity is the player, otherwise the item will attempt to add ammo to something that doesn't use ammo and cause a crash. Add this variable to the player:

Script.player=true

Then put this code on the ammo item:

function Script:Collision(entity, position, normal, speed)
	if entity.script~= nil and entity.script.player ~= nil then --Check if colliding entity is the player
		if entity.weapons[1].ammo < player.weapons[1].maxammo then
			entity.weapons[1].ammo = players.weapons[1].ammo + self.ammoamount
		end
	end
end

When an entity collides with the ammo, the script makes sure the entity has a script attached and that the entity has the "player" variable, then adds ammo if it does.

Link to comment
Share on other sites

Thank you for your reply, I was still running into issues using the code you gave me, however you put me on the right path and I now have it working. 

Thank you so much!

Script.ammoamount=6 --int "Ammo Amount"
	
function Script:Collision(entity, position, normal, speed)
	if entity.script~= nil and entity.script.player ~= nil then --Check if colliding entity is the player
		if entity.script.weapons[1].ammo< entity.script.weapons[1].maxammo then
			entity.script.weapons[1].ammo= entity.script.weapons[1].ammo + self.ammoamount
			self.entity:Release()
		end
	end
end
    
function Script:Use(player)
    if player.weapons[1].ammo<player.weapons[1].maxammo then
        player.weapons[1].ammo= player.weapons[1].ammo + self.ammoamount
        self.entity:Release()
    end
end
  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...