Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Issues with userinputservice/touchenabled, whats the deal with airplane food?

This is a regular script inside of a weapon, in a box i'm using as a weapon giver. The script works correctly when on a desktop, it gives you the "Pistol". The script also works correctly on touch devices when using "Play Here" on studio. On Local server testing or live i still am receiving the regular "Pistol", and not the desired "MobilePistol". Ive checked through the code and everything seems fine, any ideas? Thanks in advance!

	local debounce = false
	local inputservice = game:GetService("UserInputService")
	 
	local gun = game:GetService("ReplicatedStorage").VBuckWeapons.Common.Pistol -- Location of gun
	local mobgun = game:GetService("ReplicatedStorage").VBuckWeapons.Common.MobilePistol -- Location of gun

	local debouncetime = 2 -- Time of debounce
	 
	function onTouch(hit)
	    if hit.Parent:FindFirstChild("Humanoid") then
	        local plr = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
	        local char = plr.Character
	        if inputservice.KeyboardEnabled == false then
	            debounce = true
	            print("You are playing in android")
	            if char:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(mobgun.Name) or char:FindFirstChild(mobgun.Name) then
	                print("You already have a gun!")
	                wait(debouncetime)
	                debounce = false
	            else
	                mobgun:Clone().Parent = plr.Backpack
	                wait(debouncetime)
	                debounce = false   
	            end
	        else
	            debounce = true
	            gun:Clone().Parent = plr.Backpack
	            print("You playing in computer")
	            wait(debouncetime)
	            debounce = false
	            return false;
	        end
	    elseif hit.Parent.Parent:FindFirstChild("Humanoid") then
	        local plr = game:GetService("Players"):FindFirstChild(hit.Parent.Parent.Name)
	        local char = plr.Character
	        if inputservice.KeyboardEnabled == false then
	            debounce = true
	            print("You are playing in android")
	            if char:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(mobgun.Name) or char:FindFirstChild(mobgun.Name) then
	                print("You already have a gun!")
	                wait(debouncetime)
	                debounce = false
	            else
	                mobgun:Clone().Parent = plr.Backpack
	                wait(debouncetime)
	                debounce = false   
	            end
	        else
	            debounce = true
	            gun:Clone().Parent = plr.Backpack
	            print("You playing in computer")
	            wait(debouncetime)
	            debounce = false
	            return false;
	        end
	    else
	        return false;
	    end
	end
	 
	script.Parent.Parent.Touched:connect(onTouch) 

EDIT

	-- Variables --
	local debounce = false
	local debouncetime = 2
	 
	game.Players.PlayerAdded:Connect(function(plr)
	    plr.CharacterAdded:Connect(function(c)
	        local v = Instance.new("BoolValue", c)
	        v.Name = "Touched"
	        v.Value = false
	    end)
	end)
	 
	function touch(hit)
    if hit.Parent:FindFirstChild("Humanoid") and debounce == false and hit.Parent:FindFirstChild("Touched") then
	        debounce = true
	        local char = hit.Parent
	        char.Touched.Value = true
	        wait(debouncetime)
	        char.Touched.Value = false
	        debounce = false
				else	
		return end
	end
	 
	script.Parent.Touched:Connect(touch)

3 comments

0

i went through and cleaned the code up some, doesnt make a difference as far as effect though

0

You want to make on touch in a part give a gun?

0

I want it to give a weapon based on whether the player is mobile or not

1 answer

✓ Accepted answer

Not need functiont o get player, not use "else if" use "elseif" for detect player input you need to make in a local script, and make RemoveEvent, script

Here is code:

PART CODE Create a Server Script(Script) and put in your Part And put this code:

-- Variables --
local debounce = false
local debouncetime = 2

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(c)
		local v = Instance.new("BoolValue", c)
		v.Name = "Touched"
		v.Value = false
	end)
end)

function touch(hit)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == false and hit.Parent:FindFirstChild("Touched") then
		debounce = true
		local char = hit.Parent
		char.Touched.Value = true
		wait(debouncetime)
		char.Touched.Value = false
		debounce = false
	elseif hit.Parent.Parent:FindFirstChild("Humanoid") and debounce == false and hit.Parent.Parent:FindFirstChild("Touched") then
		debounce = true
		local char = hit.Parent.Parent
		char.Touched.Value = true
		wait(debouncetime)
		char.Touched.Value = false
		debounce = false
	end
end

script.Parent.Touched:Connect(touch)

EVENT CODE Now create a event named as GunGiveEvent, put GunGiveEvent in ReplicatedStorage Create a Server Script(Script) and put in ServerScriptService In this script put this code:

game.ReplicatedStorage.GunGiveEvent.OnServerEvent:Connect(function(plr,gun)
	if plr.Character:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(gun.Name) then
		return false;
	else
		gun:Clone().Parent = plr.Backpack
	end
end)

Now create a local script and put in StarterGui And put this code:

repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character
local gun = game:GetService("ReplicatedStorage").XXXX.Rare.MobilePistol -- Location of gun
local service = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character
local block = char:FindFirstChild("Touched")
local event = game:GetService("ReplicatedStorage").GunGiveEvent

if block then
	block.Changed:Connect(function()
		if block.Value == true then
			if service.TouchEnabled == true and service.KeyboardEnabled == false then
				print("ANDROID")
				game:GetService("ReplicatedStorage").GunGiveEvent:FireServer(gun)
			else
				print("COMPUTER")
			end
		else return;
		end
	end)
end

Errors? call me in the comments

17 comments

0

Worked?

0

Yes it worked, but as you said only works for first touch. is there any way to make it so it can be touched multiple times without death? I intend to make more than 1 weapon using this method!

0

Yes, you can just go in the part script and while waiting for the debounce wait you place char.Touched.Value = false, i add this on code now.

0

Done. i changed.

0

Thank you for your help!

0

14:06:22.384 - Players.Player1.PlayerGui.GunThingy:10: attempt to index local 'block' (a nil value) 14:06:22.386 - Stack Begin 14:06:22.387 - Script 'Players.Player1.PlayerGui.GunThingy', Line 10 14:06:22.388 - Stack End

0

This error popping up on Local Server/Live, works correctly without the error on play here though

0

oh i fix, wait

0

Try now, if get error i will change again.

0

is still throwing errors on " char.Touched.Value = true " line 17 on weapon giver script

0

Try again

0

Okay! I think it is close lol. Now it is throwing an error on " script.Parent.Touched:Connect(touch)". Ive edited my original post with what ive done to downsize the code, by changing the way my weapon giver was set up. hopefully this will simplify things.

0

Sorry the error thrown is "Touched is not a valid member of Tool"

0

Do you know that you should create each script for each correct weapon? and also change the name "Touched" to another name so do not have conflict with others guns.

0

What? this error is if try to get Touched on Tool lol

0

Have you tried making these changes?

0

LOL yes was accident, had script in wrong place. And yes i only have the 1 script set up not multiple. That error was resolved, now when touching giver will not do anything. Currently trying to resolve...

This discussion is preserved as it was. New answers, comments, and votes are closed.